#!/bin/bash
#

getserver() {
    case $1 in
        "2")
            be=204
            en=5000
            ;;
        "5")
            be=5001
            en=6000
            ;;
        "6")
            be=6001
            en=7000
            ;;
        "7")
            be=7001
            en=8000
            ;;
        "8")
            be=8001
            en=9000
            ;;
        *)
            echo "wrong index"
            exit 1
            ;;
    esac

    for i in `awk -F, -v be=$be '$2>be{print $2","$6}' /data/.qqslogin.profile | awk -F, -v en=$en '$1<en{print $2}' | sort | uniq`;do
        echo -n "$i:"   
        grep `echo -n $i` /data/.qqslogin.profile | awk -F, '{print $2}' | tr "\n" "," | sed -nr 's#(.*),$#\1#gp'
        num=`awk -F, -v ip=$i '$6==ip{print $2}' /data/.qqslogin.profile | wc -l | awk '{print $1}'`
        echo -n ":$num:"
        machine_mem=`awk -F: -v ip=$i '{if($1==ip)print $2}' /data/tx_machinemem.ini`
        if [ $1 == "6" ];then
            remain_mem=$[${machine_mem}-$num*6]
			remain_num=$[${remain_mem}/6]
        else
            remain_mem=$[${machine_mem}-$num*8]
			remain_num=$[${remain_mem}/8]
        fi
        echo -n ${machine_mem}:${remain_mem}:${remain_num}
        echo 

    done
}

qufucount() {
	echo $2:`getserver $1 | sort -r -t: -n -k5 | awk -F: '{sum+=$6}END{print sum}'`
	#getserver $1 | sort -r -t: -n -k5
	
}

cd /data
./create_list_zfd
#echo "各区服类型可搭服数目-------------"
case $1 in
    "rexue")
        qufucount "2" "热血服"
        ;;
    "lianmeng")
        qufucount "6" "联盟服"
        ;;
    "shengxiao")
        qufucount "5" "生肖服"
        ;;
    "xingzuo")
        qufucount "7" "星座服"
        ;;
    "diqu")
        qufucount "8" "地区服"
        ;;
	"all")
        qufucount "2" "热血服"
        qufucount "6" "联盟服"
        qufucount "5" "生肖服"
        qufucount "7" "星座服"
		;;
    *)
        echo "usage:`basename $0` 开服类型(rexue/lianmeng/xingzuo/shengxiao/all)"
        ;;
esac
